#include "Button.h"


Button::Button( SDL_Surface *sc, int pos_x, int pos_y, int width, int height, Uint32 button_color, string title)
{
	this->screen = sc;
    button_rect.x = pos_x;
    button_rect.y = pos_y;
    button_rect.w= width;
    button_rect.h = height;

    this->button_color = button_color;
    this->title = title;
}

void Button::setPosition( int pos_x, int pos_y)
{
    button_rect.x = pos_x;
    button_rect.y = pos_y;
}

void Button::setSize( int width, int height)
{
    button_rect.w= width;
    button_rect.h = height;
}

void Button::setColor( Uint32 button_color)
{
    this->button_color = button_color;
}

void Button::setTitle( string title)
{
        this->title = title;
}




void Button::draw() {
	SDL_FillRect(this->screen, &this->button_rect, this->button_color);
	std::string display = "";
	display = this->title;
    SDL_Surface* message = NULL;
	TTF_Font* font;
	font = TTF_OpenFont("Arial.ttf", 18);
	SDL_Color textColor = { 0, 0, 0};
    message = TTF_RenderText_Solid(font, display.c_str(), textColor);
	SDL_Rect tmp = this->button_rect;
    tmp.x +=5;
	SDL_BlitSurface(message, NULL, this->screen, &tmp);
	

}


bool Button::handleEvent(SDL_Event* ev){
	bool result = false;
	if(ev->type == SDL_MOUSEBUTTONDOWN && ev->button.button == SDL_BUTTON_LEFT) {
		int x = ev->button.x;
		int y = ev->button.y;
		result = this->wasClicked(x,y);
	}
    
	return result;
}


bool Button::wasClicked(int pos_x, int pos_y)
{
    if( pos_x >= button_rect.x && pos_x <= (button_rect.x + button_rect.w)
        &&  pos_y >= button_rect.y && pos_y <= (button_rect.y + button_rect.h) )
        return true;

    return false;
}


